<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/laydate/laydate.js"></script>
<script src="<?php echo URL::base() ?>media/layer/layer.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<link rel="stylesheet"  href="<?php echo URL::base() ?>media/vendor/artdialog/skins/default.css" type="text/css" media="all"/>

<div class="container-fluid">
    <form class="form-horizontal" method='get' action='<?php echo URL::site()?>checkpurchaseorder/index'>
        <div class="control-group">
            <label class="control-label">SKU:</label>
            <div class="controls">
                <input type="text" class="input2"  name="sku" value="<?php echo isset($_GET['sku'])?$_GET['sku']:''?>">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">供应商:</label>
            <div class="controls">
                <input type="text" class="input2" name="supplier_name" id="supplier_name" value="<?php echo isset($_GET['supplier_name'])?$_GET['supplier_name']:''?>">
            </div>
        </div>

         <div class="control-group">
            <label class="control-label">仓库:</label>
            <div class="controls">
                <select name="select_store" id="select_store">
                    <option value="">选择仓库</option>
                    <?php
                    if(!empty($warehouses)){
                        foreach($warehouses as $key=> $var){
                            ?>
                            <option value="<?php echo $key; ?>" <?php echo $_GET['select_store']==$key?'selected="selected"':'';?>><?php echo $var; ?></option>
                            <?php
                        }
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">主体:</label>
            <div class="controls">
                <select class="input2" name="enterprise_dominant" id="enterprise_dominant">
                    <option value="">全部</option>
                    <?php foreach ($enterprise_dominant as $key =>$val) { ?>
                        <option value="<?php echo $key;?>" <?php echo $_GET['enterprise_dominant']==$key?'selected="selected"':'';?>><?php echo $val;?></option>
                    <?php }?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">状态:</label>
            <div class="controls">
                <select name="status" class="input2">
                    <option value="">选择状态</option>
                    <?php foreach ($status as $k =>$v){ ?>
                    <option value="<?php echo $k;?>" <?php echo $_GET['status']==$k?'selected="selected"':'';?>><?php echo $v;?></option>
                    <?php } ?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">提交人:</label>
            <div class="controls">
                <input type="hidden" class="input-mini" name="create_user_id" id="create_user_id" value="<?php echo isset($_GET['create_user_id'])?$_GET['create_user_id']:''?>" autocomplete="off"/>
                <input type="text" class="input-medium" name="create_user" id="create_user" value="<?php echo isset($_GET['create_user'])?$_GET['create_user']:''?>" autocomplete="off"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">提交时间从:</label>
            <div class="controls">
                <input  class="input-medium" type="text"
                        value="<?php if (isset($_GET['date_from'])) {
                            echo $_GET['date_from'];
                        } ?>" name='date_from'/ id="date_from"/>
                到 <input  class="input-medium" type="text"
                          value="<?php if (isset($_GET['date_to'])) {
                              echo $_GET['date_to'];
                          } ?>" name='date_to' id="date_to"/>
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <input class="btn btn-info" type="submit" name="sub" value="查询"/>
                <button class="btn" type="button" name="sub"  onclick="resetdata()">重置</button>
                <input class="btn" type="submit" name="down" id="downbtn"
                       value="导出"/>
            </div>
        </div>
    </form>
    <?php
    $acl_0 = ACL::check('checkpurchaseorder_supplychain_check');        // 供应链审核权限
    $acl_1 = ACL::check('checkpurchaseorder_warehouse_check');          // 仓库操作权限
    ?>
    <table class="table table-hover ">
        <thead>
        <tr>
            <th><input type="checkbox" name="checkboxall" onclick="checkall()"></th>
            <th>供应商</th>
            <th>SKU</th>
            <th>中文名称</th>
            <th>仓库</th>
            <th>主体</th>
            <th>提交人</th>
            <th>提交时间</th>
            <th>库存量</th>
            <th>退货量</th>
            <th>审批状态</th>
            <th>备注</th>
            <th width="110">操作</th>
        </tr>
        </thead>
        <?php foreach ($result as $k=>$v){ ?>
        <tr>
            <td><input type="checkbox" <?php echo (!$acl_0 || $v['status'] > 10) ? 'disabled' : ''; ?> name="id[]" value="<?php echo $v['id']?>"><input type="hidden" name="c_status" value="<?php echo $v['status']?>"></td>
            <td><?php echo $v['supplier_name']?></td>
            <td><?php echo $v['sku']?></td>
            <td><?php echo $v['sku_name']?></td>
            <td><?php echo $warehouses[$v['warehouseid']]?></td>
            <td><?php echo $enterprise_dominant[$v['enterprise_dominant']]; ?></td>
            <td><?php echo Erpuser::nick_by_id($v['create_user_id'])?></td>
            <td><?php echo $v['create_time']?></td>
            <td><?php echo Inventory::query_sku_inventory($v['sku'],$v['supplier_id'],$v['warehouseid'],$v['enterprise_dominant'])?></td>
            <td><?php echo $v['return_qty']?></td>
            <td><?php echo $status[$v['status']]?></td>
            <td><?php echo $v['remark']?></td>
            <td>
                <?php 
                if($acl_0 && ($v['status'] == 10)){ 
                ?>
                    <a class="btn btn-success btn_operate" data-confirm="确定要通过申请吗？" href="<?php echo URL::base() ?>checkpurchaseorder/checkStatus?id=<?php echo $v['id']?>&checked=1">通过</a>
                    <a class="btn btn-danger btn_operate" data-confirm="确定要驳回申请吗？" href="<?php echo URL::base() ?>checkpurchaseorder/checkStatus?id=<?php echo $v['id']?>&checked=0">驳回</a>
                <?php 
                }elseif($acl_1 && ($v['status'] == 20)) { 
                ?>
                    <button class="btn btn-info" title="确定发货" type="button" onclick="showLog(<?php echo $v['id']?>,1)">发货</button>
                    <a class="btn btn-default btn_operate" data-confirm="确定要驳回（关闭）退货申请吗？" title="驳回（关闭）退货申请" href="<?php echo URL::base() ?>checkpurchaseorder/checkStatus?id=<?php echo $v['id']?>&checked=-1">关闭</a>
                <?php 
                } elseif($v['status'] >= 80){ 
                ?>
                    <button class="btn btn-info" type="button" onclick="showLog(<?php echo $v['id']?>,2)">查看详情</button>
                    <?php if($v['is_pull']==0){ ?>
                        <!--<button class="btn btn-info" type="button" onclick="returnPull(<?php echo $v['id']?>)">重新推送</button>-->
                    <?php }?>
                <?php 
                } else {
                    echo '无操作';
                }
                ?>
            </td>
        </tr>
        <?php }?>

    </table>
    <div class="control-group">
        <?php if($acl_0){?>
        <div class="dropdown" style="float: left;margin-left: 20px;">
            <a class="dropdown-toggle btn btn-info" data-toggle="dropdown" href="#">批量审批</a>
            <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
                <li><a tabindex="-1" onclick="javascript:checkStatusBatch(1)">审核通过</a></li>
                <li><a tabindex="-1" onclick="javascript:checkStatusBatch(0)">审核驳回</a></li>
            </ul>
        </div>
        <?php }?>
        <?php 
        if(0 && $acl_1){    // 关闭仓库批量操作
        ?>
        <div class="dropdown"  style="float: left;margin-left: 20px;">
            <a class="dropdown-toggle btn btn-info" data-toggle="dropdown" href="#">仓库审批</a>
            <ul class="dropdown-menu" role="menu3" aria-labelledby="dLabel3">
                <li><a tabindex="-1" onclick="checkStatusBatch(20,40)">审核</a></li>
                <li><a tabindex="-1" onclick="checkStatusBatch(20,50)">驳回</a></li>
            </ul>
        </div>
        <?php }?>
    </div>
<div class="clearfix"></div>
    <?php if (isset($pagination)) {
        echo $pagination;
    } ?>


</div>

<script type="text/javascript">
$(document).ready(function () {
    laydate.render({ elem: '#date_from', type: 'datetime' });
    laydate.render({elem: '#date_to', type: 'datetime'});
    $('.btn_operate').click(function(){
        var btn = $(this);
        var confirm = $(btn).attr('data-confirm') ? $(btn).attr('data-confirm') : false;
        function do_operate(btn) {
            if($(btn).hasClass('disabled')) {
                return false;
            }
            $(btn).addClass('disabled');
            var url = $(btn).attr('href');
            $.get(url, function(data){
                layer.alert(data, {closeBtn: 0},function () {
                    window.location.reload();
                });
                $(this).removeClass('disabled');
            }).error(function(){
                layer.alert('操作失败', {closeBtn: 0},function () { });
                $(btn).removeClass('disabled');
            });
        }
        if(confirm) {
            layer.confirm(confirm, {btn: ['确定','取消']}, function(){
                do_operate(btn);
            });
        } else {
            do_operate(btn);
        }
        return false;
    });
});
    $("#create_user").autocomplete({
        autoFocus: true,
        minLength: 0,
        autoFill: true,
        source: '<?php echo URL::base(true)?>purchase/get_users',
        select: function (event, ui) {
            /**
             * 从建议列表中选中之后触发的事件
             */
            $("#create_user").val(ui.item.name);
            $("#create_user_id").val(ui.item.id);
            return false;
        }
    });
    $("#create_user").blur(function(){
        $.getJSON('<?php echo URL::base(true)?>purchase/get_users', {'term':$(this).val()}, function(data){
            if($.isArray(data) && typeof(data[0]) == 'object') {
                $("#create_user").val(data[0].name);
                $("#create_user_id").val(data[0].id);
            }else {
                layer.alert('提交人不存在，请重新选择', {icon: 5});
                $("#create_user_id").val('');
                $("#create_user").val('');

            }
        });
    });
    function resetdata() {
        $(".input-medium").val("");
        $(".input-xlarge").val("");
        $(".input-mini").val("");
        $(".input2").val("");
//        $("#status").val("1");
    }
    function checkStatusBatch(checked) {
        var id =[],msg ='';
        $('input[name="id[]"]:checked').each(function () {
            id.push($(this).val());
        });
        if(id.length==0){
            layer.alert('请选择数据', {icon: 5});
            return false;
        }
        layer.confirm('你确定要进行此操作？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.get("<?php echo URL::base() ?>checkpurchaseorder/checkStatus",
                {id: id, checked:checked},
                function (data) {
                    layer.alert(data, {closeBtn: 0},function () {
                        window.location.reload();
                    });
                });
        }, function(){
        });

    }
    function checkall() {
        $('input[name=checkbox]').each(function () {
            if(this.checked){
                $(this).prop('checked',false);
            }else{

                $(this).prop('checked',true);
            }
        });
    }
    function showLog(id,type) {
        var url = "<?php echo url::base(true);?>checkpurchaseorder/returnShow";
        var title = '供应商退货发货确认';
        if(type==2){
            title = '供应商退货物流信息';
        }
        $.get(url,
            {id: id,link:$('#form').serialize()},
            function (data) {
                art.dialog({
                    title:title,
                    lock: true,
                    opacity: 0.87,	// 透明度
                    content: data,
                    width: '45%',
                    height: '60%',
                    button: [{
                        name: '关闭',
                        callback: function () {

                        }
                    }]
                });
            });
    }
    function returnPull(p_id) {
        layer.confirm('你确定要重新推送吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.get("<?php echo URL::base() ?>checkpurchaseorder/pull",
                {id: p_id},
                function (data) {
                    layer.alert(data, function () {
                        window.location.reload();
                    });
                });
        }, function(){
        });
    }
</script>
